#include "present_blocks.h"
#include "utility.h"
#include <iostream>
using namespace std;

present_blocks::present_blocks()
{
    hash_to_position = new std::map<uint64_t, std::string::size_type>();
}

present_blocks::~present_blocks()
{
    delete hash_to_position;
}

void present_blocks::from_sequence(const string &_sequence, string::size_type _length, string::size_type max_size)
{
    //sequence = _sequence;
    length = _length;
    //cerr << hash_to_position.size() << endl;
    hash_to_position->clear();
    for(string::size_type position = 0 ; position <= _sequence.length() - length ; position++)
    {
        (*hash_to_position)[utility::hash_function(_sequence.substr(position,length))] = position;
        if(hash_to_position->size() == max_size)
            break;
    }

}
void present_blocks::swap(present_blocks &other)
{
    string::size_type dummy_length = length;
    length = other.length;
    other.length = dummy_length;

    std::map<uint64_t, std::string::size_type> *dummy = this->hash_to_position;
    hash_to_position = other.hash_to_position;
    other.hash_to_position = dummy;

    this->reset();
}
void present_blocks::reset()
{
    current_iterator = hash_to_position->begin();
}
void present_blocks::next()
{
    current_iterator++;
}
std::string present_blocks::current(const string &_sequence)
{
    return _sequence.substr(current_iterator->second, this->length);
}
bool present_blocks::end()
{
    return current_iterator == hash_to_position->end();
}
uint64_t present_blocks::size()
{
    return this->hash_to_position->size();
}

bool present_blocks::contains(const std::string &str)
{
    return this->hash_to_position->find(utility::hash_function(str)) != hash_to_position->end();
}
